package com.zsh.wanandroid.base;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.api.ApiService;
import com.zsh.wanandroid.bean.ArticleBean;
import com.zsh.wanandroid.bean.ArticleTags;
import com.zsh.wanandroid.bean.BaseResult;
import com.zsh.wanandroid.bean.PageResult;
import com.zsh.wanandroid.bean.ProjectBean;
import com.zsh.wanandroid.bean.ProjectTagsBean;
import com.zsh.wanandroid.bean.ProjectTreeBean;
import com.zsh.wanandroid.http.ApiRequest;
import com.zsh.wanandroid.utils.UIUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zsh
 * @date 2020/3/18.
 * 功能：
 */

public class BasePagerAdapter extends PagerAdapter {

    private Context mContext;
    private List<ProjectTreeBean> mTList;
    private View rootView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private MyBaseQuickAdapter mAdapter;

    private final static int DEFAULT_PAGE = 1;//默认加载第1页
    private int currentPage = DEFAULT_PAGE;//当前页数，默认第一页

    public BasePagerAdapter(Context context, List<ProjectTreeBean> tList) {
        mContext = context;
        mTList = tList;
    }

    @Override
    public int getCount() {
        return mTList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        rootView = View.inflate(mContext, R.layout.fragment_common, null);
        container.addView(rootView);
        init();
        getNet(currentPage, mTList.get(position).getId());
        return rootView;
    }

    private void init() {
        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeRefreshLayout);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new MyBaseQuickAdapter(R.layout.score_item);
        recyclerView.setAdapter(mAdapter);
    }

    private void getNet(int pages, int cid) {
        ApiService apiService = ApiRequest.getInstance(mContext).create(ApiService.class);
        apiService.getProjectList(pages, cid).
                enqueue(new Callback<BaseResult<PageResult<ArticleBean<ArticleTags>>>>() {
                    @Override
                    public void onResponse(Call<BaseResult<PageResult<ArticleBean<ArticleTags>>>> call,
                                           Response<BaseResult<PageResult<ArticleBean<ArticleTags>>>> response) {
                        BaseResult<PageResult<ArticleBean<ArticleTags>>> body = response.body();
                        if (body != null) {
                            PageResult<ArticleBean<ArticleTags>> data = body.getData();
                            currentPage = data.getCurPage();
                            if (null != data) {
                                List<ArticleBean<ArticleTags>> datas = data.getDatas();
                                if (null == datas) {
                                    UIUtils.showToast("暂无数据");
                                    return;
                                }
                                //mAdapter.addData(datas);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResult<PageResult<ArticleBean<ArticleTags>>>> call, Throwable t) {
                        UIUtils.showToast("网络错误");
                    }
                });
    }

    private class MyBaseQuickAdapter extends BaseQuickAdapter<ProjectBean<ProjectTagsBean>, BaseViewHolder> {

        public MyBaseQuickAdapter(@LayoutRes int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, ProjectBean<ProjectTagsBean> item) {
            helper.setText(R.id.tvReason, item.getTitle());
            helper.setText(R.id.tvDesc, item.getNiceDate()+"_"+item.getId());
        }
    }
}
